<template>
  <view class="goods_detail" v-if="goodsDetail.goods_name">
    <swiper class="swiper" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
      <swiper-item v-for="(item,index) in goodsDetail.pics">
        <image @click="previewImage(index)" :src="item.pics_big" mode=""></image>
      </swiper-item>

    </swiper>
    <!-- 介绍 -->
    <view class="info">
      <view class="price">
        ￥{{goodsDetail.goods_price | toFixed}} {{test}}
      </view>
      <view class="msg">
        <view class="left">
          小米(MI)小米电视4 2GB+8GB 55英寸
        </view>
        <view class="right">
          <uni-icons type="star" size="20"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <view class="bottom">
        快递：免运费
      </view>
    </view>
    <!-- 详情图片 -->
    <rich-text :nodes="goodsDetail.goods_introduce"></rich-text>
    <view class="goods-carts">
      <uni-goods-nav :options="options" :fill="true" :button-group="buttonGroup" @click="onClick"
        @buttonClick="buttonClick" />
    </view>
  </view>
</template>

<script>
  import {
    mapState,
    mapMutations,
    mapGetters
  } from 'vuex'
  export default {
    computed: {
      ...mapState('cart', ['test']),
      ...mapGetters('cart',['total'])
    },
    // 用监听器修改购物车的count值
    watch:{
      total:{
        handler:function(){
          this.options[1].info =this.total
        },
         immediate: true // 开启页面时，立即执行一次
      }
    },
    data() {
      return {
        goodsDetail: {},
        // 商品导航左侧结构的数据源
        options: [{
            icon: 'shop',
            text: '店铺'
          },
          {
            icon: 'cart',
            text: '购物车',
            info: 3,
            infoBackgroundColor: '#ff0000',
            infoColor: '#fff'
          }
        ],
        // 商品导航右侧结构的数据源
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ],
        goods_id: ''
      }
    },
    onLoad({
      goods_id
    }) {
      this.getDetail(goods_id),
        this.goods_id = goods_id
        console.log(this.total,'total');
    },

    filters: {
      toFixed(v) {
        return v.toFixed(2)
      }
    },
    methods: {
      ...mapMutations('cart', ['addToCart']),
      // 加入购物车
      buttonClick({
        index
      }) {
        if (index === 0) {
        this.addToCart({
          goods_id:this.goodsDetail.goods_id,
          goods_name:this.goodsDetail.goods_name,
          goods_price:this.goodsDetail.goods_price,
          goods_small_logo:this.goodsDetail.goods_small_logo,
          goods_count:1,
          goods_status:true
        })
        uni.showToast({
          title:'加入成功'
        })
        }
      },
      // 点击购物车
      onClick({
        index
      }) {
        if (index === 1) {
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }

      },
      // 预览图片
      previewImage(index) {
        // 得到所有的图片的地址
        const newArr = this.goodsDetail.pics.map(item => item.pics_big)
        // 实现预览全图
        uni.previewImage({
          // 预览时，默认显示的图片索引
          current: index,
          // 索引图片的url地址的数值
          urls: newArr
        })
      },
      async getDetail(goods_id) {
        const {
          data: {
            message,
            meta: {
              status,
              msg
            }
          }
        } = await uni.$http.get(`/goods/detail?goods_id=${goods_id}`)
        if (status !== 200) return uni.$showMsg(msg)
        this.goodsDetail = message
        this.goodsDetail.goods_introduce = message.goods_introduce.replace(/<img /g, '<img style="display:block"')
        this.goodsDetail.goods_introduce = message.goods_introduce.replace(/.webp/gi, '.jpg')
      }
    }
  }
</script>

<style lang="scss">
  .goods_detail {
    padding-bottom: 50px;

    .swiper {
      height: 750rpx;

      image {
        width: 100%;
        height: 750rpx;
      }
    }

    .info {
      padding: 20rpx;

      .price {
        color: #c00000;

        height: 60rpx;
        line-height: 60rpx;
      }

      .msg {
        display: flex;
        font-size: 14px;

        .left {
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
        }

        .right {
          width: 200rpx;
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;
          border-left: 1px solid #ccc;
          margin-left: 20rpx;
          box-sizing: border-box;
        }

      }

      .bottom {
        font-size: 12px;
        color: #968fa9;
        line-height: 60rpx;
      }


    }

    .goods-carts {
      position: fixed;
      bottom: 0;
      left: 0;
      width: 100%;
    }

  }
</style>
